package com.fzy.core.system.entity;

import ge.core.system.entity.Property;

import com.fzy.core.utils.BooleanUtils;
import com.fzy.core.utils.DateUtils;
import com.fzy.core.utils.StringUtils;
import com.fzy.core.utils.BuilderUtils;

public class PropertyWrapper {
	public static final String STRING = "string";
	public static final String LONG = "long";
	public static final String DATE = "date";
	public static final String NUMBER = "number";
//	public static final String DICT = "dict";
//	public static final String REF = "ref";
	
	public static final String JAVA_STRING = "String";
	public static final String JAVA_LONG = "Long";
	public static final String JAVA_DATE = "Date";
	public static final String JAVA_DOUBLE = "Double";

	
	public static final int LENGTH_DEFAULT = 255;
	public static final int LENGTH_MIN = 0;
	public static final int LENGTH_MAX = 4096;
	public static final int PRECISION_DEFAULT = 38;
	public static final int PRECISION_MIN = 0;
	public static final int PRECISION_MAX = 38;
	public static final int SCALE_DEFAULT = 0;
	public static final int SCALE_MIN = 0;
	public static final int SCALE_MAX = 38;
	Property data;
	EntityWrapper parent;
	
	
	public String getCommentText() {
		String result = "";
		String comment = data.getCommentText();
		if(StringUtils.isBlank(comment)) return result;
		comment = comment.trim();
		int firstParentheses = comment.indexOf("(");
		if(firstParentheses > -1){
			comment = comment.substring(0, firstParentheses);
		}
		result = comment;
		if("0".equals(data.getIsWork())){
			result += "(deleted:废弃)";
		}
		if(getIsKey() && getHasSequence()){
			result += "(sequence:"+StringUtils.trimToEmpty(data.getSequenceName()).toLowerCase()+")";
		}
		if(getIsDict()){
			String dict = StringUtils.trimToEmpty(data.getDictType());
			result += "(dict:"+dict+")";
			if("yn".equalsIgnoreCase(dict)){
				result += "(0,否,1,是)";
			}
		}
		if(getHasDefaultValue()){
			result += "(default:"+data.getDefaultValue()+")";
		}
		if(!StringUtils.isBlank(data.getRefTable()) && !StringUtils.isBlank(data.getRefColumn())){
			result += "(reference:"+StringUtils.trimToEmpty(data.getRefTable()).toLowerCase() + "."+StringUtils.trimToEmpty(data.getRefColumn()).toLowerCase()+")";
		}
		
		if(!StringUtils.isBlank(data.getDateFormat())){
			result += "(format:"+data.getDateFormat()+")";
		}
		
//		if(!StringUtils.isBlank(data.getDateFormatShow())){
//			result += "(formatShow:"+data.getDateFormatShow()+")";
//		}
		return result;
	}
	
	public boolean getIsSavedField(){
		return BuilderUtils.isSavedProperty(getNameUncapital());
	}
	
	public boolean getHasDateFormat(){
		return !StringUtils.isBlank(data.getDateFormat());
	}
	
	public boolean getHasDateFormatShow(){
		return !StringUtils.isBlank(data.getDateFormatShow());
	}
	
	public String getDateFormatShow(){
		String result = null;
		if(!StringUtils.isBlank(data.getDateFormatShow()))
			result = data.getDateFormatShow();
		else if(JAVA_DATE.equals(getJavaType())){
			result = DateUtils.PATTERN_SHOW_LONG;
		}
		return result;
	}
	
	public boolean getHasSequence(){
		return !StringUtils.isBlank(data.getSequenceName());
	}
	
	public boolean getHasDefaultValue(){
		return !StringUtils.isBlank(data.getDefaultValue());
	}
	
	public boolean getIsNotNull(){
		return getIsKey() ? true : BooleanUtils.toBoolean(data.getIsNotNull(), false);
	}
	
	public boolean getIsKey(){
		return BooleanUtils.toBoolean(data.getIsKey(), false);
	}
	
	public boolean getIsDict(){
		return !StringUtils.isBlank(data.getDictType());
	}
	
	public boolean getIsUnique(){
		return BooleanUtils.toBoolean(data.getIsUnique(), false);
	}
	
	public boolean getIsShow(){
		return BooleanUtils.toBoolean(data.getIsShow(), true);
	}
	
	public boolean getIsQuery(){
		return BooleanUtils.toBoolean(data.getIsQuery(), false);
	}
	
	public boolean getIsStatus(){
		return "status".equals(data.getNameText());
	}
	
	public boolean getIsCreatedUser(){
		return "createdUser".equals(data.getNameText());
	}
	
	public boolean getIsCreatedDate(){
		return "createdDate".equals(data.getNameText());
	}
	
	public boolean getIsUpdatedUser(){
		return "updatedUser".equals(data.getNameText());
	}
	
	public boolean getIsUpdatedDate(){
		return "updatedDate".equals(data.getNameText());
	}
	
	public long getPrecisionValue(){
		long result = data.getPrecisionValue() == null ? PRECISION_DEFAULT : data.getPrecisionValue().longValue();
		if(result < PRECISION_MIN) result = PRECISION_MIN;
		if(result > PRECISION_MAX) result = PRECISION_MAX;
		return result;
	}
	
	public long getLengthValue(){
		long result = data.getLengthValue() == null ? LENGTH_DEFAULT : data.getLengthValue().longValue();
		if(result < LENGTH_MIN) result = LENGTH_MIN;
		if(result > LENGTH_MAX) result = LENGTH_MAX;
		return result;
	}
	
	public long getScaleValue(){
		long result = data.getScaleValue() == null ? SCALE_DEFAULT : data.getScaleValue().longValue();
		if(result < SCALE_MIN) result = SCALE_MIN;
		if(result > SCALE_MAX) result = SCALE_MAX;
		return result;
	}
	
	public String getHbmType(){
		String result = "";
		if(STRING.equalsIgnoreCase(getPropertyType()))
			result = "type=\"string\" length=\""+getLengthValue()+"\"";
		else if(LONG.equalsIgnoreCase(getPropertyType()))
			result = "type=\"long\"";
		else if(DATE.equalsIgnoreCase(getPropertyType()))
			result = "type=\"timestamp\"";
		else if(NUMBER.equalsIgnoreCase(getPropertyType()))
			result = "type=\"double\" precision=\""+getPrecisionValue()+"\" scale=\""+getScaleValue()+"\"";
//		else if(DICT.equalsIgnoreCase(getPropertyType()))
//			result = "type=\"string\" length=\""+LENGTH_DEFAULT+"\"";
//		else if(REF.equalsIgnoreCase(getPropertyType()))
//			result = "type=\"string\" length=\""+LENGTH_DEFAULT+"\"";
		
		return result;
	}
	
	public String getDbType(){
		String result = "";
		if(STRING.equalsIgnoreCase(getPropertyType()))
			result = "varchar("+getLengthValue()+")";
		else if(LONG.equalsIgnoreCase(getPropertyType()))
			result = "bigint";
		else if(DATE.equalsIgnoreCase(getPropertyType()))
			result = "timestamp";
		else if(NUMBER.equalsIgnoreCase(getPropertyType())){
			if(getPrecisionValue() == PRECISION_DEFAULT && getScaleValue() == SCALE_DEFAULT)
				result = "number";
			else
				result = "number("+getPrecisionValue()+", "+getScaleValue()+")";
		}
//		else if(DICT.equalsIgnoreCase(getPropertyType()))
//			result = "varchar("+LENGTH_DEFAULT+")";
//		else if(REF.equalsIgnoreCase(getPropertyType()))
//			result = "varchar("+LENGTH_DEFAULT+")";

		return result;
	}
	
	public String getDbOracleType(){
		String result = "";
		if(STRING.equalsIgnoreCase(getPropertyType()))
			result = "varchar2("+getLengthValue()+")";
		else if(LONG.equalsIgnoreCase(getPropertyType()))
			result = "integer";
		else if(DATE.equalsIgnoreCase(getPropertyType()))
			result = "date";
		else if(NUMBER.equalsIgnoreCase(getPropertyType())){
			if(getPrecisionValue() == PRECISION_DEFAULT && getScaleValue() == SCALE_DEFAULT)
				result = "number";
			else
				result = "number("+getPrecisionValue()+", "+getScaleValue()+")";
		}
//		else if(DICT.equalsIgnoreCase(getPropertyType()))
//			result = "varchar2("+LENGTH_DEFAULT+")";
//		else if(REF.equalsIgnoreCase(getPropertyType()))
//			result = "varchar2("+LENGTH_DEFAULT+")";

		return result;
	}
	
	public String getPropertyType(){
		String type = StringUtils.isBlank(data.getPropertyType()) ? STRING : data.getPropertyType().trim().toLowerCase();
		if(NUMBER.equals(type) && getScaleValue() < 1 && getIsKey())
			type = LONG;
		return type;
	}
	
	public String getJavaType() {
		String result = getPropertyType();
		if(NUMBER.equalsIgnoreCase(result))
			result = "Double";
//		if(DICT.equalsIgnoreCase(result))
//			result = STRING;
//		if(REF.equalsIgnoreCase(result))
//			result = STRING;

		return StringUtils.capitalize(result);
	}
	
	public String getColumnUpperCase() {
		return getColumnName().toUpperCase();
	}
	
	public String getColumnName() {
		String result;
		if(!StringUtils.isBlank(data.getColumnName())){
			result = data.getColumnName();
		}else{
			result = com.fzy.core.utils.StringUtils.camel2underline(data.getNameText());
		}
		return result.toLowerCase();
	}
	
	public String getNameCapital() {
		return StringUtils.capitalize(StringUtils.trim(data.getNameText()));
	}
	
	public String getNameUncapital() {
		return StringUtils.uncapitalize(StringUtils.trim(data.getNameText()));
	}
	////////////////////////////////// generated /////////////////////////////////////////////
	public Property getData() {
		return data;
	}
	public void setData(Property data) {
		this.data = data;
	}
	public EntityWrapper getParent() {
		return parent;
	}
	public void setParent(EntityWrapper parent) {
		this.parent = parent;
	}
	
}
